<template>
  <div class="updateName">
    <!-- 头部导航 -->
    <van-nav-bar
      title="更新昵称"
      left-text="取消"
      right-text="保存"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <!-- 文本域 -->
    <van-field
      v-model="nickname"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserProfileAPI } from '@/api'
export default {
  name: 'updataNickname',
  components: {},
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      nickname: this.name
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    onClickLeft() {
      this.$emit('close')
    },
    async onClickRight() {
      this.$toast.loading({
        message: '更新昵称中...',
        forbidClick: true
      })
      await editUserProfileAPI({ name: this.nickname })
      // 更新视图
      this.$emit('update:name', this.nickname)
      this.$emit('close')
      this.$toast.success('更新昵称成功！')
    }
  }
}
</script>

<style scoped lang="less">
.updateName {
  :deep(.van-nav-bar__content) {
    background-color: #fff;
    .van-nav-bar__title {
      color: #b0b0b1;
    }
    .van-icon-arrow-left {
      color: #b0b0b1;
    }
  }
}
</style>
